/*
 *****************************************************************************
 * Copyright (C) 2005-2011 UC Mobile Limited. All Rights Reserved
 * File			: BaseAutomatedPage.m
 *
 * Description	: 常用自动化测试页面基类
 *
 * Author		: daijb@ucweb.com
 * History		:
 *			   Creation, 2014/03/14, daijb, Create the file
 ******************************************************************************
 **/
#if defined(UC_COMMON_AUTOMATED_TEST) || defined(UC_MONKEY_TEST)
#import "VerDefine.h"

#import "BaseAutomatedPage.h"
#import "CommonAutomatedInfoData.h"

@interface BaseAutomatedPage()


@end

@implementation BaseAutomatedPage

#pragma mark - life

- (instancetype)initWithTitle:(NSString*)title
{
    if (self = [super init])
    {
        self.title = title;
    }
    
    return self;
}

- (void)dealloc
{
    [_dataProvider release];
    _dataProvider = nil;
    
    _dataProcessor.delegate = nil;
    [_dataProcessor release];
    _dataProcessor = nil;
    
    [super dealloc];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self getAutomatedItemCount];
}

#pragma mark - Table view delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    const AutomatedItemInfo* itemInfoArray = [self.dataProvider automatedItemInfo];
    AutomatedItemInfo itemInfo = itemInfoArray[indexPath.row];
    
    static NSString* cellIndentifier = @"cellIndentifier";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier] autorelease];
        cell.textLabel.text = itemInfo.m_itemName;
        cell.textLabel.accessibilityLabel = itemInfo.m_accessibilityLabel;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    int index = indexPath.row;
    int itemCount = [self getAutomatedItemCount];
    if (itemCount > index)
    {
        const AutomatedItemInfo* itemInfoArray = [self.dataProvider automatedItemInfo];
        SEL selector = itemInfoArray[index].m_callbackSel;
        if ([self.dataProcessor respondsToSelector:selector])
        {
            [self.dataProcessor performSelector:selector withObject:itemInfoArray[index].m_itemName];
        }
    }
    
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}

- (int)getAutomatedItemCount
{
    if (nil == self.dataProvider)
    {
        return 0;
    }
    
    const AutomatedItemInfo* itemInfo = [self.dataProvider automatedItemInfo];
    
    int count = 0;
    while (itemInfo[count].m_itemName != nil)
    {
        count++;
    }
    
    return count;
}

@end
#endif
